package com.ruoyi.maode.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.exception.UtilException;
import com.ruoyi.maode.domain.dto.QueryPartDTO;
import com.ruoyi.maode.domain.dto.QuerySupplierDTO;
import com.ruoyi.maode.domain.entity.Part;
import com.ruoyi.maode.domain.entity.Supplier;
import com.ruoyi.maode.service.SupplierService;
import com.ruoyi.web.core.config.ResultDTO;
import com.ruoyi.web.util.PageQuery;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 供应商Controller
 * @author zhangwenqiang
 */

@RestController
@RequestMapping("/supplier")
public class SupplierController extends BaseController {

    @Autowired
    private SupplierService supplierService;

    @ApiOperation("新增供应商")
    @PostMapping("/add")
    public ResultDTO add(@RequestBody Supplier supplier) {
        return supplierService.add(supplier) > 0 ? ResultDTO.success("新增成功！") : ResultDTO.error("新增失败！");
    }

    @ApiOperation("删除供应商")
    @DeleteMapping("/delete")
    public ResultDTO delete(@RequestBody Long id) {
        return ResultDTO.success(supplierService.removeById(id));
    }

    @ApiOperation(value = "批量删除供应商")
    @DeleteMapping("/deleteByIds")
    public ResultDTO deleteByIds(@RequestBody List<Long> ids) {
        boolean result = supplierService.removeByIds(ids);
        return result  ? ResultDTO.success() : ResultDTO.error();
    }

    @ApiOperation("修改供应商")
    @PutMapping("/edit")
    public ResultDTO update(@RequestBody Supplier supplier) {
        return ResultDTO.success(supplierService.updateById(supplier));
    }

    @ApiOperation("查询详情")
    @GetMapping("/detail/{id}")
    public ResultDTO getSupplierById(@PathVariable("id")Long id) {
        return ResultDTO.success(supplierService.getById(id));
    }

    @ApiOperation("查询供应商列表")
    @PostMapping("/list")
    public ResultDTO<Page<Supplier>> getPage(@RequestBody PageQuery<QuerySupplierDTO> dto) {
        Page<Supplier> supplierList = supplierService.selectSupplierList(dto);
        return ResultDTO.success(supplierList);
    }

    @ApiOperation("根据供应商名称，模糊查询")
    @GetMapping("/getBySupplierName/{supplierName}")
    public ResultDTO<List<Supplier>> getBySupplierName(@PathVariable("supplierName")String supplierName) {
        List<Supplier> list = supplierService.list(new LambdaQueryWrapper<Supplier>()
                .like(Supplier::getSupplierName, supplierName));
        return ResultDTO.success(list);
    }
}
